package cn.tedu.system.oa.stamp.controller;


import cn.tedu.system.oa.base.response.JsonResult;
import cn.tedu.system.oa.stamp.pojo.dto.StampQuery;
import cn.tedu.system.oa.stamp.pojo.entity.Stamp;
import cn.tedu.system.oa.stamp.pojo.param.StampAddParam;
import cn.tedu.system.oa.stamp.service.StampService;
import com.alibaba.excel.EasyExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "03.印章管理")
@RequestMapping("/oa/stamp")
@Slf4j
public class StampController {

    @Autowired
    private StampService stampService;

    @PostMapping("insert")
    @ApiOperation("新建+编辑印章")
    public JsonResult saveStamp(StampAddParam stampAddParam) {
        log.debug("新建印章:stampAddParam:={}", stampAddParam);

        stampService.saveStamp(stampAddParam);

        return JsonResult.ok();
    }

    @PostMapping("delete/{id}")
    @ApiOperation("删除印章")
    public JsonResult deleteStamp(@PathVariable Long id) {
        log.debug("删除印章:id={}", id);

        stampService.deleteById(id);

        return JsonResult.ok();
    }

    @GetMapping("select")
    @ApiOperation("查询印章")
    public JsonResult selectStamp(StampQuery stampQuery) {
        log.debug("查询印章:stampQuery={}", stampQuery);

        List<Stamp> list = stampService.selectList(stampQuery);

        return JsonResult.ok(list);
    }

    @GetMapping("print")
    @ApiOperation("导出印章表格")
    public JsonResult printStamp(StampQuery stampQuery) {
        log.debug("查询导出印章:stampQuery={}", stampQuery);

        List<Stamp> list = stampService.selectList(stampQuery);


        long time = System.currentTimeMillis();
        String filename = "E:\\Files\\stamp" + time + ".xlsx";

        // 向Excel中写入数据 也可以通过 head(Class<?>) 指定数据模板
        EasyExcel.write(filename, Stamp.class)
                .sheet("印章信息")
                .doWrite(list);

        log.debug("导出印章表格成功");

        return JsonResult.ok();
    }


}




